<?php
/**
 * Smarty扩展函数
 */

/**
 * 按照长度截短字符串，支持中文
 *
 * @param unknown_type $params
 * @return unknown
 */
function smartyCutstr($params) {
	$dot = "...";
	if ($params ['str'] && is_numeric ( $params ['length'] ) && $params ['length'] > 1) {
		if ($params ['dot'] != '') {
			$dot = $params ['dot'];
		}
		return cutstr ( $params ['str'], $params ['length'], $dot );
	} else {
		return $params ['str'];
	}
}

/**
 * 将关键字在一段文本中高亮
 *
 * @param unknown_type $args
 * @return unknown
 */
function smartyHeighKeyword($args) {
	$text = $args ['content'];
	$arKeyword = $args ['arKeyword'];
	$length = 100;
	if ($arKeyword [0]) {
		$text = _smartyHeighKeyword_ext ( $arKeyword [0], $text, $length );
		foreach ( $arKeyword as $keyword ) {
			$exp [] = "/($keyword)/i";
			$pat [] = "<font color=\"#ff4e00\">$1</font>";
		}
		return preg_replace ( $exp, $pat, $text );
	} else {
		return cutstr ( $text, $length );
	}
}
function _smartyHeighKeyword_ext($keyword, $string, $length) {
	if (! $string || ! $keyword) {
		return $string;
	}
	$tArr = explode ( $keyword, $string );
	if (count ( $tArr ) > 0) {
		$s1_lgh = length ( $tArr [0] );
		$s2 = $tArr [1];
		$tArr [1] = cutstr ( $tArr [1], $length, '' );
		$_bl_str = cutstr ( $string, ($s1_lgh - $length), '' );
		$string = str_replace ( $_bl_str, '', $string );
		$string = str_replace ( $s2, '', $string );
		$strcut = cutstr ( $string, ($s1_lgh + $length), '' ) . $tArr [1];
	}
	return $strcut;
}
